﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using The_Square_Saga.Levels;
using The_Square_Saga.Utils;

namespace The_Square_Saga.Objects
{
    // this class works like a container for the objects in the game.
    class World
    {
        /// <summary>
        /// The list of all game entitys in the game
        /// </summary>
        private List<GameEntity> gameEntitys;

        /// <summary>
        /// The size of the world
        /// </summary>
        private Vector2 dimensions;
        
        private Level level;

        /// <summary>
        /// World constructor
        /// </summary>
        /// <param name="level"> the current level of this world</param>
        /// <param name="dimensions">The size</param>
        public World(Level level,Vector2 dimensions)
        {
            gameEntitys = new List<GameEntity>();
            this.dimensions = dimensions;
            this.level = level;
        }


        public void addGameEntity(GameEntity gE)
        {
            gameEntitys.Add(gE);
        }

        public void removeGameEntity(GameEntity gE)
        {
            gameEntitys.Remove(gE);
        }

        /// <summary>
        /// Keep the game entity in the limits of the world
        /// </summary>
        /// <param name="gE">A game entity</param>
        private void keepInWorld(GameEntity gE)
        {
            keepInWorldGameEntity(gE);
        }

        private void keepInWorldGameEntity(GameEntity gE)
        {
            if (gE.CollisionRect.Right >= this.dimensions.X - gE.CollisionRect.Width)
            {
                gE.position.X = this.dimensions.X - gE.CollisionRect.Width;
            }

            if (gE.CollisionRect.Bottom >= this.dimensions.Y - gE.CollisionRect.Height)
            {
                gE.position.Y = this.dimensions.Y - gE.CollisionRect.Height;
            }

            if (gE.CollisionRect.Left <= 0)
            {
                gE.position.X = 0;
            }

            if (gE.CollisionRect.Top <= 0)
            {
                gE.position.Y = 0;
            }
        }

        public void update(GameTime gameTime)
        {
            // update other entitys
            foreach (GameEntity gE in gameEntitys)
            {
                gE.update(gameTime);

            }
        }

        public void draw(SpriteBatch spriteBatch)
        {
            foreach (GameEntity gE in gameEntitys)
            {
                gE.draw(spriteBatch);
            }
        }
    }
}
